package yxy.game.pm2.command.union.leader;

import yxy.cherry.data.string.GameString;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionLeaderMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.UnionLog;
import yxy.game.pm2.module.union.player.UnionPlayer;

/** 联盟盟主 联盟踢人 */
@MessageType(proto = Command.UnionExpel, classtype = UnionLeaderMessage.UnionExpelRequest.class)
public class CMD_UnionExpel extends Command<UnionLeaderMessage.UnionExpelRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionLeaderMessage.UnionExpelRequest request) {
		String playerCode = messageSource.getTag();
		String code = request.getCode();

		UnionLeaderMessage.UnionExpelResponse.Builder response = UnionLeaderMessage.UnionExpelResponse.newBuilder();

		if (playerCode.equals(code)) {
			send(messageSource, response, Result.ParamError);// 不能踢自己
			return;
		}
		// 管理员权限
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnionManager(entry);
		if (!result.equals(Result.UnionManager)) {
			send(messageSource, response, result);
			return;
		}
		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		RedisLock pLock = UnionPlayer.lock(code);
		try {
			if (uLock.tryLock() && pLock.tryLock()) {
				UnionBean union = entry.reloadUnionBean();
				UnionPlayer manager = entry.reloadUnionPlayer();

				if (union.getViceList().contains(code)) {
					send(messageSource, response, Result.UnionViceCantExpel);// 副会长不能被踢
					return;
				}
				if (!union.getMemberList().contains(code)) {
					send(messageSource, response, Result.UnionTargetNotMember);// 这人不是成员
					return;
				}

				UnionPlayer unionPlayer = UnionPlayer.get(code);
				// 成员操作
				unionPlayer.setUnionCode(null);
				unionPlayer.set();

				// 联盟操作
				union.getMemberList().remove(code);
				union.set();
				union.active();

				String oppoCode = unionPlayer.getPlayerCode();
				PlayTypeHandle.saveUnion(oppoCode, PlayTypeModule.get(oppoCode));
				new BattleHandle().powerUpdate(oppoCode);
				new UnionHandle().powerUpdate(union);

				send(messageSource, response, Result.Success);

				// 联盟日志
				PlayerBean mplayer = PlayerBean.get(manager.getPlayerCode());
				PlayerBean player = PlayerBean.get(unionPlayer.getPlayerCode());
				UnionLog.add(union.getCode(), GameString.联盟日志被踢(mplayer.getName(), player.getName()).getText());

				// 联盟状态推送
				new UnionHandle().unionPlayerNotification(unionPlayer.getPlayerCode());
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
			pLock.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionLeaderMessage.UnionExpelResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}